<template>
  <div v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading">
    <div class="btn">
      <el-button type="primary" @click="open_duakig">添加轮播图数据</el-button>
    </div>
    <!-- 上传轮播图数据 -->
    <div>
      <el-dialog
        title="添加轮播图数据"
        :visible.sync="dialogVisible"
        :modal="true"
        :modal-append-to-body="true"
        :close-on-click-modal="false"
      >
        <!-- 上传组件 -->
        <el-upload
          class="upload-demo"
          drag
          action="#"
          :http-request="carouselUpload"
          multiple
          :show-file-list="false"
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            将文件拖到此处，或
            <em>点击上传</em>
          </div>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过1024KB</div>
        </el-upload>
        <!-- 上传组件 -->

        <!-- 表单组件 -->
        <div>
          <el-form ref="form" :model="form" label-width="80px" :rules="rules">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" size="mini" :disabled="inputis"></el-input>
            </el-form-item>
          </el-form>
        </div>
        <!-- 表单组件 -->

        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="carouselAdd" :disabled="btnIsok">确 定</el-button>
        </span>
      </el-dialog>
    </div>

    <!-- 对轮播图操作 -->
    <div>
      <el-table :data="tableData" style="width: 100%" border :stripe="true">
        <el-table-column prop="ID" label="id" width="180"></el-table-column>
        <el-table-column prop="carousel_name" label="名称" width="180"></el-table-column>
        <el-table-column prop="sort" label="排序"></el-table-column>
        <el-table-column prop="url" label="url"></el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small" icon="el-icon-edit"></el-button>
            <el-button
              type="text"
              size="small"
              icon="el-icon-delete"
              @click="carouseldelete(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="URL" label="url"></el-table-column> -->
      </el-table>
    </div>
  </div>
</template>

<script>
import { CarouselLists, CarouselAdd, CarouseDelete } from "@/utils/http.js";
export default {
  data() {
    return {
      tableData: [],
      loading: true,
      dialogVisible: false, // 弹出框
      form: {}, // 轮播图添加数据
      rules: {
        sort: [{ required: true, message: "请输入排序值", trigger: "blur" }]
      },
      carouselList: {}, //formData数据
      inputis: false,
      btnIsok: false
    };
  },
  methods: {
    // 轮播图数据加载

    carouselAll() {
      this.loading = true;
      CarouselLists()
        .then(res => {
          if (res.data.code == 200) {
            this.tableData = res.data.data;
            this.form = {};
            this.dialogVisible = false;
            this.loading = false;
          } else {
            this.loading = false;

            this.tableData = [];
            this.form = {};
            this.dialogVisible = false;
            this.$message({
              message: res.data.message,
              type: "warning",
              center: true
            });
            this.loading = false;
          }
        })
        .catch(err => {
          this.$message.error(err);
        });
    },

    // 触发上传请求
    carouselUpload(item) {
      let new_type = item.file.name.split(".");
      if (
        new_type[new_type.length - 1] == "png" ||
        new_type[new_type.length - 1] == "jpg" ||
        new_type[new_type.length - 1] == "jpeg"
      ) {
        this.inputis = false;
        this.btnIsok = false;
        this.carouselList = item.file;
      } else {
        this.$message({
          message: "文件类型错误",
          type: "warning",
          center: true
        });
        this.inputis = true;
        this.btnIsok = true;
      }

      if (item.file.size / 1024 <= 1024 * 5) {
        this.inputis = false;
        this.btnIsok = false;
        this.carouselList = item.file;
      } else {
        this.$message({
          message: "上传的文件太大",
          type: "warning",
          center: true
        });
        this.inputis = true;
        this.btnIsok = true;
      }
    },

    // 轮播图添加弹窗打开事件
    open_duakig() {
      this.dialogVisible = true;
    },
    carouselAdd() {
      let formData = new FormData();
      formData.append("carousel", this.carouselList);
      CarouselAdd(formData, this.form.sort)
        .then(res => {
          if (res.data.code == 200) {
            this.carouselAll();
            this.loading = false;
          } else {
            this.loading = false;
            this.tableData = [];
            this.$message({
              message: res.data.message,
              type: "warning",
              center: true
            });
            this.loading = false;
          }
        })
        .catch(err => {
          this.loading = false;

          this.$message.error(err);
        });
    },
    // 弹窗取消事件
    cancel() {
      this.form = {};
      this.dialogVisible = false;
    },
    // 删除事件
    carouseldelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.loading = true;
          CarouseDelete(row.ID)
            .then(res => {
              if (res.data.code == 200) {
                this.loading = false;
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.carouselAll();
              } else {
                this.loading = false;
                this.$message({
                  type: "success",
                  message: res.data.message
                });
              }
            })
            .catch(err => {
              this.loading = false;
              this.$message.error(err);
            });
        })
        .catch(() => {
          // this.$message({
          //   type: "info",
          //   message: "已取消删除"
          // });
        });
    }
  },
  mounted() {
    this.carouselAll();
  }
};
</script>

<style lang="less" scoped>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
.btn {
  margin-bottom: 10px;
}
</style>
